package com.bdqn.boot.yuanshen.service.impl;

import com.bdqn.boot.yuanshen.bean.Users;
import com.bdqn.boot.yuanshen.mapper.UsersMapper;
import com.bdqn.boot.yuanshen.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fly
 * @version 1.0
 * @className UsersServiceImpl
 * @description TODO
 * @date 2025/3/12 09:01
 */
@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;


    @Override
    public void addUsers(Users users) {
        usersMapper.addUsers(users);
    }

    @Override
    public void deleteUser(Integer id) {
        usersMapper.deleteUser(id);
    }

    @Override
    public void updateUser(Users users) {
        usersMapper.updateUser(users);
    }

    @Override
    public PageInfo<Users> getUsers(int pageNum, int pageSize) {
        // 开启分页功能
        PageHelper.startPage(pageNum, pageSize);
        List<Users> usersList = usersMapper.getUsers();
        return new PageInfo<>(usersList);
    }

    @Override
    public PageInfo<Users> getUsers(String username, int pageNum, int pageSize) {
        List<Users> usersList = usersMapper.getUserByUserNameAndPage(username);
        return new PageInfo<>(usersList);
    }

    @Override
    public Users getUser(Integer id) {
        return usersMapper.getUserById(id);
    }


    @Override
    public Users login(String username, String password) {
        Users users = usersMapper.login(username);
        if (users == null) {
            throw new RuntimeException("用户名不存在");
        }
        if (!users.getPassword().equals(password)){
            throw new RuntimeException("密码错误");
        }
        return users;
    }

    @Override
    public Users getUserById(Integer userId) {
        return usersMapper.getUserById(userId);
    }
}
